<script setup>
import {ref, reactive} from 'vue';
import {notification} from "ant-design-vue";
import {cloneDeep} from 'lodash-es';
import userApi from '@/api/userApi';
import {PASSENGER_TYPE_ARRAY} from '@/assets/js/enums'

const visible = ref(false);
const loading = ref(false);

let passenger = ref({
  id: undefined,
  memberId: undefined,
  name: undefined,
  idCard: undefined,
  type: undefined,
  createTime: undefined,
  updateTime: undefined,
});

const passengers = ref([]);
const pagination = reactive({
  total: 0,
  current: 1,
  pageSize: 2
})

const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '身份证',
    dataIndex: 'idCard',
    key: 'idCard',
  },
  {
    title: '旅客类型',
    dataIndex: 'type',
    key: 'type',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  },
]
const handleQuery = (param) => {
  if (!param) {
    param = {
      "page": 1,
      "size": pagination.pageSize
    }
  }
  loading.value = true;
  userApi.queryPassengerList({
    "page": param.page,
    "limit": param.size
  }).then(res => {
    console.log('res', res)
    loading.value = false;
    passengers.value = res.data;
    pagination.total = res.count;
    pagination.current = res.page
  })
}

const handleTableChange = (pagination) => {
  handleQuery({
    page: pagination.current,
    size: pagination.pageSize
  })
}

handleQuery()

const onAdd = () => {
  passenger.value = {};
  visible.value = true;
}

const onEdit = (record) => {
  console.log("record", record)
  passenger.value = cloneDeep(record);
  visible.value = true;
}

const onDelete = (record) => {
  console.log("delete record", record)
  userApi.deletePassenger(record.id).then(() => {
    notification.success({description: "删除成功！"});
    handleQuery({
      page: pagination.current,
      size: pagination.pageSize,
    });
  })
}

const handleOk = () => {
  userApi.savePassenger(passenger.value).then(resp => {
    notification.success({description: "保存成功！"});
    visible.value = false;
    handleQuery({
      page: pagination.current,
      size: pagination.pageSize
    })
  })
}
</script>

<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <a-table :dataSource="passengers" :columns="columns" :pagination="pagination" @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, text, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a @click="onEdit(record)">编辑</a>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              ok-text="确认" cancel-text="取消"
              @confirm="onDelete(record)"
          >
            <a style="color: red">删除</a>
          </a-popconfirm>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'type'">
        <span v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.type">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <a-modal v-model:open="visible" title="乘车人" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form
        :model="passenger" :label-col="{span: 4}" :wrapper-col="{ span: 20 }"
    >
      <a-form-item label="姓名">
        <a-input v-model:value="passenger.name"/>
      </a-form-item>
      <a-form-item label="身份证">
        <a-input v-model:value="passenger.idCard"/>
      </a-form-item>
      <a-form-item label="旅客类型">
        <a-select v-model:value="passenger.type">
          <a-select-option v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{ item.desc }}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>

</template>